package com.app.teaware.bean;


import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import my.StringUtil;
import my.TimeUtils;

public class ParamsEditListBean extends BasisBean {

    public static final int FORM_FIR_ALARM_FEEDBACK = 1, FORM_DUTY_NEWS = 2;

    private ArrayList<ParamsEditItemData> list = new ArrayList<ParamsEditItemData>();

    private HashMap<String, String> mapParams = new HashMap<>();

    private boolean isEdit = false;
    private int type_form = 0;

    public ArrayList<ParamsEditItemData> getList() {
        return list;
    }

    public void setList(ArrayList<ParamsEditItemData> list) {
        this.list = list;
    }

    public HashMap<String, String> getMapParams() {
        return mapParams;
    }

    public void setMapParams(HashMap<String, String> mapParams) {
        this.mapParams = mapParams;
    }

    @JSONField(serialize = false)
    public boolean isGroup() {
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() != -1) return true;
        }
        return false;
    }

    @JSONField(serialize = false)
    public int getNextGroupPostion() {
        int position = 0;
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() >= position) {
                position = itemData.getGroup_position() + 1;
            }
        }
        return position;
    }

    @JSONField(serialize = false)
    public void addNextGroup() {
        int position_next = getNextGroupPostion();
        ArrayList<ParamsEditItemData> listAdd = new ArrayList<>();
        ParamsEditItemData editItemData;
        int itemCount = 0;
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() == position_next - 1) {
                editItemData = new ParamsEditItemData();
                editItemData.setName_show(itemData.getName_show());
                editItemData.setName(itemData.getName());
                editItemData.setGroup_position(position_next);
                editItemData.setEdit_type(itemData.getEdit_type());
                if (editItemData.getEdit_type() == ParamsEditItemData.EDIT_TYPE_TITLE) {
                    editItemData.setName_show(itemData.getName() + (position_next + 1));
                }
                editItemData.setChoose_items(itemData.getChoose_items());
                editItemData.setHint(itemData.getHint());
//                editItemData.setInputtype(itemData.getInputtype());
                editItemData.setUnit(itemData.getUnit());
                editItemData.setValue_name(itemData.getValue_name());
                editItemData.setMax_length(itemData.getMax_length());
                if (editItemData.getEdit_type() == ParamsEditItemData.EDIT_TYPE_TIME) {
                    editItemData.setListTimeBoolean(itemData.getListTimeBoolean());
                    editItemData.setTimeFormat(itemData.getTimeFormat());
                    editItemData.setValue(new Date().getTime()+"");
                }
                listAdd.add(editItemData);
            }
            if (listAdd.size() != 0 && itemData.getGroup_position() == -1) {
                break;
            }
            itemCount++;
        }
//        if()
        list.addAll(itemCount, listAdd);
    }


    @JSONField(serialize = false)
    public void putParams(String name, String value) {
        mapParams.put(name, value);
    }

    @JSONField(serialize = false)
    public String getTitle() {
        return mapParams.get("title");
    }


    @JSONField(serialize = false)
    public void setEditChange(int position, String content) {
        if (position > list.size()) return;
        list.get(position).setValue(content);
    }

    @JSONField(serialize = false)
    public boolean isEditEmpty() {
        for (ParamsEditItemData data : list) {
            if(data.getEdit_type() == ParamsEditItemData.EDIT_TYPE_DIIVIDER || data.getEdit_type() == ParamsEditItemData.EDIT_TYPE_TITLE)continue;
                if (TextUtils.isEmpty(data.getValue())) return true;
        }
        return false;
    }

    @JSONField(serialize = false)
    public String getEmptyItemName() {
        for (ParamsEditItemData data : list) {
            if (data.getEdit_type() == ParamsEditItemData.EDIT_TYPE_TITLE   || data.getEdit_type() == ParamsEditItemData.EDIT_TYPE_DIIVIDER) {
            } else {
                if (TextUtils.isEmpty(data.getValue())) return data.getName_STR();
            }
        }
        return "";
    }

    @JSONField(serialize = false)
    public void onChildItemChoose(int position, ParamsChooseItemData data) {
        if (list.size() <= position) return;
        list.get(position).setValue(data.getValue());
        list.get(position).setValue_name(data.getValue_name());
    }

    @JSONField(serialize = false)
    public void onChildItemChoose(int position, String value, String valueStr) {
        if (list.size() <= position) return;
        list.get(position).setValue(value);
        list.get(position).setValue_name(valueStr);
    }

    @JSONField(serialize = false)
    public void onChildItemBooleanChange(int position) {
        if (list.size() <= position) return;
        boolean isCheck = StringUtil.Str2Boolean(list.get(position).getValue());
        list.get(position).setValue(StringUtil.Boolean2StrZero(!isCheck));
    }

    @JSONField(serialize = false)
    public void checkVisiable() {
        ParamsEditItemData data;
        for (int i = 0, l = list.size(); i < l; i++) {
            data = list.get(i);
            if (!TextUtils.isEmpty(data.getRelate_name())) {
//                if(data.getName_show().equals("主服务器通道")){
//                    LogUtil.i("主服务器通道--->"+data.getRelate_name()+"--->"+data.getRelate_name_value());
//                }
                for (ParamsEditItemData itemSub : list) {

                    if (data.getRelate_name().equals(itemSub.getName())) {
//                        LogUtil.i("checkVisiable--->"+data.getRelate_name());
                        data.setVisiable(itemSub.getValue().matches(data.getRelate_name_value()));
                    } else if (data.getRelate_name().equals(itemSub.getName_show())) {
//                        LogUtil.i("checkVisiable--->"+data.getRelate_name());
                        data.setVisiable(itemSub.getValue().matches(data.getRelate_name_value()));
                    }
//                    if(itemSub.getName_show().equals("服务器")){
//                        LogUtil.i("服务器--的值是->"+itemSub.getValue());
//                        LogUtil.i("  data.setVisiable->"+data.isVisiable());
//                    }
                }
            }
        }
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public int getType_form() {
        return type_form;
    }

    public void setType_form(int type_form) {
        this.type_form = type_form;
    }


    @JSONField(serialize = false)
    public String getPostJsonArrayStr() {
        JSONArray jsonArray = new JSONArray();

        JSONObject jsonobject;
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() == -1) continue;
            if (TextUtils.isEmpty(itemData.getName())) continue;
            if (itemData.getGroup_position() + 1 > jsonArray.size()) {
                jsonobject = new JSONObject();
//                jsonobject.put("serial",itemData.getGroup_position()+1 );
                jsonArray.add(jsonobject);
            } else {
                jsonobject = jsonArray.getJSONObject(itemData.getGroup_position());
            }
            jsonobject.put(itemData.getName(), itemData.getValue());
        }


        return jsonArray.toJSONString();
    }

    @JSONField(serialize = false)
    public JSONArray getPostJsonArray() {
        JSONArray jsonArray = new JSONArray();

        JSONObject jsonobject;
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() == -1) continue;
            if (itemData.getEdit_type() == ParamsEditItemData.EDIT_TYPE_TITLE ) continue;
            if (TextUtils.isEmpty(itemData.getName())) continue;
            if (itemData.getGroup_position() + 1 > jsonArray.size()) {
                jsonobject = new JSONObject();
//                jsonobject.put("serial",itemData.getGroup_position()+1 );
                jsonArray.add(jsonobject);
            } else {
                jsonobject = jsonArray.getJSONObject(itemData.getGroup_position());
            }
            if(itemData.getEdit_type() == ParamsEditItemData.EDIT_TYPE_TIME){
                jsonobject.put(itemData.getName(), itemData.getTIME_STR());
            }else {
                jsonobject.put(itemData.getName(), itemData.getValue());
            }

        }


        return jsonArray;
    }

    public ParamsEditItemData getItemDataByName(String name) {
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() != -1) continue;
            if (itemData.getName().equals(name)) {
                return itemData;
            }
        }
        return null;
    }

    public String getItemDataValueByName(String name) {
        for (ParamsEditItemData itemData : list) {
            if (itemData.getGroup_position() != -1) continue;
            if (itemData.getName().equals(name)) {
                return itemData.getValue();
            }
        }
        return null;
    }

    public String getItemTimeByNameAndFormat(String name,String fomat) {
        String value = getItemDataValueByName(name);
        if(TextUtils.isEmpty(value))return "";
        try {
            long timel = Long.valueOf(value);
            return TimeUtils.getTimeLongToStrByFormat(timel,fomat);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
